// src/store.js
import { createStore } from "vuex";
import stack from "@/utils/stack";

const store = createStore({
  state() {
    return {
      rootPath: "/",
      forwardStack: new stack(), //前进栈
      backStack: new stack(), //后退栈
    };
  },
  mutations: {
    goForward(state) {
      const data = state.forwardStack.pop();
      if (data && data != "") {
        state.backStack.push(data);
      }
    },
    goBack(state) {
      const data = state.backStack.pop();
      if (data && data != "") {
        state.forwardStack.push(data);
      }
    },
    newPage(state, data) {
      let top = state.backStack.peek();
      top = top + data + "/";
      state.backStack.push(top);
    },
    setBackStack(state, data) {
      const top = state.backStack.peek();
      state.forwardStack.push(top);
      state.backStack.push(data);
    },
    resetStack(state) {
      state.backStack.clear();
      state.forwardStack.clear();
    },
    resetForward(state) {
      state.forwardStack.clear();
    },
  },
  actions: {},
  getters: {
    getBackTop(state) {
      return state.backStack.peek();
    },
    getForwardTop(state) {
      return state.forwardStack.peek();
    },
    getBackSize(state) {
      return state.backStack.size();
    },
    getForwardSize(state) {
      return state.forwardStack.size();
    },
  },
});

export default store;
